package com.liuzhou.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import com.liuzhou.dao.PlanDao;
import com.liuzhou.dao.SysparaDao;
import com.liuzhou.dao.TypeDao;
import com.liuzhou.dao.UserDao;
import com.liuzhou.dao.impl.PlanDaoImpl;
import com.liuzhou.dao.impl.SysparaDaoImpl;
import com.liuzhou.dao.impl.TypeDaoImpl;
import com.liuzhou.dao.impl.UserDaoImpl;
import com.liuzhou.model.Plan;
import com.liuzhou.model.User;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class PlanOperationAction extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		JSONObject jsonObject = null;
		JSONArray jsonArray = new JSONArray();

		// 从前端取值
		String msg = req.getParameter("msg");
		List<Plan> list = new ArrayList<Plan>();
		Plan plan = new Plan();
		if (msg.equals("combineQuery")) {
			plan.setId(Integer.parseInt(req.getParameter("id")));
			plan.setName(req.getParameter("name"));
			plan.setContent(req.getParameter("content"));
			plan.setSign(req.getParameter("sign"));
			plan.setSyspara_id(Integer.parseInt(req.getParameter("syspara_id")));
			plan.setPersonnel_id(Integer.parseInt(req.getParameter("personnel_id")));
			plan.setType_id(Integer.parseInt(req.getParameter("type_id")));
			plan.setBegintime(req.getParameter("begintime"));
			plan.setEndtime(req.getParameter("endtime"));
			try {
				list = CombineQuery(plan);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else if (msg.equals("monthQuery")) {
			try {
				list = MonthQuery();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (msg.equals("yearQuery")) {
			try {
				list = YearQuery();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (msg.equals("add")) {
			
			plan.setName(req.getParameter("name"));
			plan.setContent(req.getParameter("content"));
			plan.setSign(req.getParameter("sign"));
			plan.setSyspara_id(Integer.parseInt(req.getParameter("syspara_id")));
			plan.setPersonnel_id(Integer.parseInt(req.getParameter("personnel_id")));
			plan.setType_id(Integer.parseInt(req.getParameter("type_id")));
			plan.setBegintime(req.getParameter("begintime"));
			plan.setEndtime(req.getParameter("endtime"));
			boolean b = false;
			try {
				b = addPlan(plan);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(b == false){
				System.out.println("addPlan error  line89");
			}else{
				JSONObject j = new JSONObject();
				j.put("state", "success1");
				PrintWriter out = resp.getWriter();
				out.println(j);
				out.flush();
				out.close();
				return;
			}
		} else if (msg.equals("delete")) {
			plan.setId(Integer.parseInt(req.getParameter("id")));
//			plan.setName(req.getParameter("name"));
//			plan.setContent(req.getParameter("content"));
//			plan.setSign(req.getParameter("sign"));
//			plan.setSyspara_id(Integer.parseInt(req.getParameter("syspara_id")));
//			plan.setPersonnel_id(Integer.parseInt(req.getParameter("personnel_id")));
//			plan.setType_id(Integer.parseInt(req.getParameter("type_id")));
//			plan.setBegintime(req.getParameter("begintime"));
//			plan.setEndtime(req.getParameter("endtime"));
			boolean b = false;
			try {
				b = deletePlan(plan);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(b == false){
				System.out.println("deletePlan error   line109");
			}else{
				JSONObject j = new JSONObject();
				j.put("state", "success2");
				PrintWriter out = resp.getWriter();
				out.println(j);
				out.flush();
				out.close();
				return;
			}
		} else if (msg.endsWith("update")) {
			plan.setId(Integer.parseInt(req.getParameter("id")));
			plan.setName(req.getParameter("name"));
			plan.setContent(req.getParameter("content"));
			plan.setSign(req.getParameter("sign"));
			plan.setSyspara_id(Integer.parseInt(req.getParameter("syspara_id")));
			plan.setPersonnel_id(Integer.parseInt(req.getParameter("personnel_id")));
			plan.setType_id(Integer.parseInt(req.getParameter("type_id")));
			plan.setBegintime(req.getParameter("begintime"));
			plan.setEndtime(req.getParameter("endtime"));
			boolean b = false;
			try {
				b = updatePlan(plan);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(b == false){
				System.out.println("deletePlan error   line109");
			}else{
				JSONObject j = new JSONObject();
				j.put("state", "success3");
				PrintWriter out = resp.getWriter();
				out.println(j);
				out.flush();
				out.close();
				return;
			}
		} else {
			System.out.println("你的msg传的有问题，不知道你要干什么！");
			return;
		}
		// 返回值

		for (int i = 0; i < list.size(); i++) {
			jsonObject = new JSONObject();
			plan = list.get(i);
			jsonObject.put("id", plan.getId());
			jsonObject.put("name", plan.getName());
			jsonObject.put("content", plan.getContent());
			jsonObject.put("sign", plan.getSign());
			jsonObject.put("syspara_id", plan.getSyspara_id());
			jsonObject.put("syspara_name", getSyspara_name(plan.getSyspara_id()));
			jsonObject.put("personnel_id", plan.getPersonnel_id());
			jsonObject.put("personnel_name", getPersonnel_name(plan.getPersonnel_id()));
			jsonObject.put("type_id", plan.getType_id());
			jsonObject.put("type_name", getType_name(plan.getType_id()));
			jsonObject.put("begintime", plan.getBegintime());
			jsonObject.put("endtime", plan.getEndtime());
			jsonArray.add(jsonObject);
		}
		PrintWriter out = resp.getWriter();
		out.println(jsonArray);
		out.flush();
		out.close();
	}

	// 混合查询
	public List<Plan> CombineQuery(Plan plan) throws SQLException {
		PlanDao planDao = new PlanDaoImpl();
		List<Plan> list = planDao.queryPlan(plan);
		System.out.println("xxxxxxxxxxxxxxxx");
		return list;
	}

	// 月度查询
	public List<Plan> MonthQuery() throws SQLException {
		PlanDao planDao = new PlanDaoImpl();
		Plan plan = new Plan();
		plan.setSign("月度");
		List<Plan> list = planDao.queryPlan(plan);
		return list;
	}

	// 年度查询
	public List<Plan> YearQuery() throws SQLException {
		PlanDao planDao = new PlanDaoImpl();
		Plan plan = new Plan();
		plan.setSign("年度");
		List<Plan> list = planDao.queryPlan(plan);
		return list;
	}

	// 添加
	public boolean addPlan(Plan plan) throws SQLException {
		PlanDao planDao = new PlanDaoImpl();
		boolean b = planDao.addPlan(plan);
		return b;
	}
	// 删除
	public boolean deletePlan(Plan plan) throws SQLException {
		PlanDao planDao = new PlanDaoImpl();
		boolean b = planDao.deletePlan(plan);
		return b;
	}
	// 修改
	public boolean updatePlan(Plan plan) throws SQLException {
		PlanDao planDao = new PlanDaoImpl();
		boolean b = planDao.updatePlan(plan);
		return b;
	}

	
	//一下三个为通过id 获取名称
	public String getSyspara_name(int id) {
		SysparaDao sysparaDao = new SysparaDaoImpl();
		String string = "";
		try {
			string = sysparaDao.queryNameById(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (string.equals("")) {
			System.out.println("getSyspara_name 出错了 在123行！");
			return null;
		}
		return string;
	}

	public String getPersonnel_name(int id) {
		UserDao userDao = new UserDaoImpl();
		String string = "";
		try {
			string = userDao.queryUserById(id).getName();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (string.equals("")) {
			System.out.println("getPersonnel_name 出错了 在123行！");
			return null;
		}
		return string;
	}

	public String getType_name(int id) {
		TypeDao typeDao = new TypeDaoImpl();
		String string = "";
		try {
			string = typeDao.queryNameById(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (string.equals("")) {
			System.out.println("getType_name 出错了 在123行！");
			return null;
		}
		return string;
	}

}
